package com.quan.base.common.base.service.support;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quan.base.common.base.dao.support.BaseMapper;
import com.quan.base.common.exception.BizException;
import com.quan.base.common.utils.Paginator;

/**
 * Service实现类可以继承该基础类，该类封装了CRUD操作
 * @author yhaoquan
 *
 * @param <T>
 * @param <PK>
 */
public class BaseService<T, PK> implements IService<T, PK> {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BaseMapper<T, PK> baseMapper;

	@Override
	public T insert(T entity) throws BizException {
		try {
			this.baseMapper.insert(entity) ;
			return entity ;
		} catch (Exception e) {
			throw BizException.DB_INSERT_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public T insertSelective(T entity) throws BizException {
		try {
			this.baseMapper.insertSelective(entity) ;
			return entity ;
		} catch (Exception e) {
			throw BizException.DB_INSERT_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public List<T> insertBatch(List<T> entityList) throws BizException {
		try {
			this.baseMapper.insertList(entityList);
			return entityList;
		} catch (Exception e) {
			throw BizException.DB_INSERT_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public int delete(PK id) throws BizException {
		try {
			return this.baseMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			throw BizException.DB_DELETE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public int deleteSelective(T entity) throws BizException {
		try {
			return this.baseMapper.delete(entity);
		} catch (Exception e) {
			throw BizException.DB_DELETE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public int deleteByIds(List<PK> idList) throws BizException {
		try {
			return this.baseMapper.deleteBatchIds(idList);
		} catch (Exception e) {
			throw BizException.DB_DELETE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public int deleteByIds(String ids) throws BizException {
		try {
			return this.baseMapper.deleteByIds(ids) ;
		} catch (Exception e) {
			throw BizException.DB_DELETE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public T update(T entity) throws BizException {
		try {
			this.baseMapper.updateByPrimaryKey(entity);
			return entity;
		} catch (Exception e) {
			throw BizException.DB_UPDATE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public T updateSelective(T entity) throws BizException {
		try {
			this.baseMapper.updateByPrimaryKeySelective(entity);
			return entity;
		} catch (Exception e) {
			throw BizException.DB_UPDATE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public T update(T entity, T whereEntity) throws BizException {
		try {
			this.baseMapper.update(entity, whereEntity);
			return entity;
		} catch (Exception e) {
			throw BizException.DB_UPDATE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public T updateSelective(T entity, T whereEntity) throws BizException {
		try {
			this.baseMapper.updateSelective(entity, whereEntity);
			return entity;
		} catch (Exception e) {
			throw BizException.DB_UPDATE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public List<T> updateBatchById(List<T> entityList) throws BizException {
		try {
			this.baseMapper.updateBatchById(entityList);
			return entityList;
		} catch (Exception e) {
			throw BizException.DB_UPDATE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public T selectById(PK id) throws BizException {
		try {
			return this.baseMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			throw BizException.DB_SELECTONE_EXCEPTION.errorMsg(e) ;
		}
	}
	
	@Override
	public T selectOne(T entity) throws BizException {
		try {
			return this.baseMapper.selectOne(entity);
		} catch (Exception e) {
			throw BizException.DB_SELECTONE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public int selectCount(T entity) throws BizException {
		try {
			return this.baseMapper.selectCount(entity);
		} catch (Exception e) {
			throw BizException.DB_SELECTONE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public List<T> selectByIds(List<PK> idList) throws BizException {
		try {
			return this.baseMapper.selectBatchIds(idList);
		} catch (Exception e) {
			throw BizException.DB_SELECTONE_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public List<T> selectByIds(String ids) throws BizException {
		try {
			return this.baseMapper.selectByIds(ids) ;
		} catch (Exception e) {
			throw BizException.DB_SELECTONE_EXCEPTION.errorMsg(e) ;
		}
	}


	@Override
	public List<T> selectAll() throws BizException {
		try {
			return this.baseMapper.selectAll() ;
		} catch (Exception e) {
			throw BizException.DB_LIST_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public List<T> selectList(T entity) throws BizException {
		try {
			return this.baseMapper.select(entity) ;
		} catch (Exception e) {
			throw BizException.DB_LIST_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public List<T> selectList(T entity, String orderBy) throws BizException {
		try {
			return this.baseMapper.selectList(entity, orderBy) ;
		} catch (Exception e) {
			throw BizException.DB_LIST_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public PageInfo<T> selectPage(Paginator paginator) throws BizException {
		return this.selectPage(paginator, null) ;
	}

	@Override
	public PageInfo<T> selectPage(Paginator paginator, T entity) throws BizException {
		try {
			Page<T> page = PageHelper.startPage(paginator.getPage(), paginator.getRows()) ;
			this.baseMapper.selectPage(paginator, entity) ;
			return new PageInfo<T>(page) ;
		} catch (Exception e) {
			throw BizException.DB_LIST_EXCEPTION.errorMsg(e) ;
		}
	}

	@Override
	public T checkValue(String field, Object value) throws BizException {
		try {
			return this.baseMapper.checkValue(field, value) ;
		} catch (Exception e) {
			throw new BizException("检查字段值异常，字段不存在==》字段：%s，值：%s", field, value).errorMsg(e) ;
		}
	}


}
